package com.jzo2o.orders.manager.handler;

import cn.hutool.core.collection.CollUtil;
import com.jzo2o.common.constants.UserType;
import com.jzo2o.orders.base.enums.OrderPayStatusEnum;
import com.jzo2o.orders.base.enums.OrderStatusEnum;
import com.jzo2o.orders.base.model.domain.Orders;
import com.jzo2o.orders.manager.model.dto.OrderCancelDTO;
import com.jzo2o.orders.manager.service.impl.OrdersManagerServiceImpl;
import com.jzo2o.orders.manager.strategy.OrderCancelStrategyManager;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

public class cancelOverTimePayOrder {

    @Autowired
    private OrdersManagerServiceImpl ordersManagerService;
    @Autowired
    private OrderCancelStrategyManager orderCancelStrategyManager;

    /**
     * 取消超时订单
     */
    @XxlJob("cancelOverTimePayOrder")
    public void cancelOverTimePayOrder() {
        //1. 查询超时未支付的订单
        //select * from orders where orders_status = 0 and pay_status = 2 and create_time < 当前时间 - 15分钟
        List<Orders> list = ordersManagerService.lambdaQuery()
                //orders_status = 0
                .eq(Orders::getOrdersStatus, OrderStatusEnum.NO_PAY.getStatus())
                //pay_status = 2
                .eq(Orders::getPayStatus, OrderPayStatusEnum.NO_PAY.getStatus())
                //create_time < 当前时间 - 15分钟
                .lt(Orders::getCreateTime, LocalDateTime.now().minusMinutes(15))
                //限制每次最多查100条
                .last("limit 100")
                .list();
        if (CollUtil.isEmpty(list)) {
            return;
        }
        //2. 遍历集合, 获取到每一笔订单
        for (Orders orders : list) {
            //然后去取消
            OrderCancelDTO orderCancelDTO = new OrderCancelDTO();
            //订单id
            orderCancelDTO.setId(orders.getId());
            //当前用户id
            orderCancelDTO.setCurrentUserId(0L);
            //当前用户名称
            orderCancelDTO.setCurrentUserName("系统定时任务");
            //当前用户类型
            orderCancelDTO.setCurrentUserType(UserType.SYSTEM);
            //取消原因
            orderCancelDTO.setCancelReason("超时未支付");
            orderCancelStrategyManager.cancel(orderCancelDTO);
        }
    }
}